<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\SetParentForm;
use App\Admin\Controllers\Helpers\LinkHelper;
use App\Models\Adreward;
use App\Models\User;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AdrewardController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('个人红包收益')
            ->description('')
            ->body(function (Row $row) {
                $row->column(4, '');
                $row->column(4, '');
                $row->column(4, '');
            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Adreward(), function (Grid $grid) {
            $grid->quickSearch('id', 'user_id','user.uuid','user.nickname')->placeholder('输入 id\uuid\用户昵称\uid')->auto(false);;


            $grid->model()->with(['user','parent','member']);
            $grid->model()->orderBy('created_at', 'desc');
            $grid->column('id')->sortable();
            $grid->column('user_id')->display(function () {
                return LinkHelper::displayUser($this->user);
            });
            $grid->column('user.parent_id', '来源(上级)')
                ->width('200')
                ->display(function () {


                    $parent = User::query()->find($this->user->parent_id);
                    $leader = $this->member->leader ?? '';

                    $text = '<i>-</i>';
                    if ($parent) {
                        $text = "师傅: <a href='?id={$parent->id}'>{$parent->nickname}｜<b>{$parent->uuid}</b></a>";
                    }
                    if ($leader) {
                        $text .= "<br/> 团长: <a href='?id={$leader->id}'>{$leader->nickname}｜<b>{$leader->uuid}</a>";
                    }
                    return "<div style='font-size: smaller'>{$text}</div>";
                });


            $grid->column('amount')->display(function ($amount) {
                return "<a href='adreward-details?adreward_id={$this->id}'>{$amount}</a>";
            })->sortable();
            $grid->column('received_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user_id');
            });

            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableActions();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Adreward(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('amount');
            $show->field('received_at');
        });
    }
}
